﻿using System;
using VideoServices.Domain.Entities;
using System.Linq;

namespace VideoServices.Domain.DAL
{
    public interface IDeliveryDataAccess
    {
        /// <summary>
        /// Gets the delivery by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        Delivery GetDeliveryById(Guid id);

        /// <summary>
        /// Creates the delivery.
        /// </summary>
        /// <param name="newDelivery">The new delivery.</param>
        /// <returns></returns>
        Delivery CreateDelivery(Delivery newDelivery);
    }

    public class DeliveryDataAccess : BaseDataAccess, IDeliveryDataAccess
    {
        #region Implementation of IDeliveryDataAccess

        /// <summary>
        /// Gets the delivery by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Delivery GetDeliveryById(Guid id)
        {
            Delivery delivery = DataContext.Deliveries.SingleOrDefault(c => c.id == id);
            return delivery;
        }

        /// <summary>
        /// Creates the delivery.
        /// </summary>
        /// <param name="newDelivery">The new delivery.</param>
        /// <returns></returns>
        public Delivery CreateDelivery(Delivery newDelivery)
        {
            newDelivery.Order = DataContext.Orders.Single(o => o.id == newDelivery.order_id);
            newDelivery.Warehouse = DataContext.Warehouses.Single(w => w.id == newDelivery.warehouse_id);
            DataContext.Deliveries.InsertOnSubmit(newDelivery);
            DataContext.SubmitChanges();
            return newDelivery;
        }

        #endregion
    }
}
